//
//  IBHelper.m
//  Halalgems
//
//  Created by Nguyen Minh Khoai on 12/20/12.
//  Copyright (c) 2012 Nguyen The Phu. All rights reserved.
//

#import "IBHelper.h"

@implementation IBHelper
+ (IBHelper *)sharedUIHelper {
    static IBHelper *sharedUIHelper = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if(!sharedUIHelper) {
            sharedUIHelper = [[IBHelper alloc] init];
        }
    });
    return sharedUIHelper;
}

- (NSString *)correctName:(NSString *)name iPad:(BOOL)iPad {
    if(iPad) {
        return [NSString stringWithFormat:@"%@%@", name, (IS_IPAD ? @"_iPad" : @"")];
    }
    return [NSString stringWithFormat:@"%@", name];
}

- (NSString *)imageName:(NSString *)name iPad:(BOOL)iPad {
    return [NSString stringWithFormat:@"%@.png", [self correctName:name iPad:iPad]];
}

- (UIImage *)loadImage:(NSString *)name {
    return [UIImage imageNamed:[self imageName:name iPad:YES]];
}

- (UIImage *)stretchImagedName:(NSString *)imageName leftPadding:(CGFloat)leftPadding topPadding:(CGFloat)topPadding {
    return [Utils stretchImagedName:[self imageName:imageName iPad:YES] leftPadding:leftPadding topPadding:topPadding];
}

- (UIImage *)stretchImagedName:(NSString *)imageName topPadding:(CGFloat)padding {
    return [Utils stretchImagedName:[self imageName:imageName iPad:YES] topPadding:padding];
}

- (UIImage *)middleStretchImage:(NSString *)name {
    return [Utils middleStretchImagedName:[self imageName:name iPad:YES]];
}

- (UIImage *)tiledImageName:(NSString *)imageName edgeInset:(UIEdgeInsets)inset {
    return [Utils tiledImageName:[self imageName:imageName iPad:YES] edgeInset:inset];
}


/* common images */
- (NSString *)imageCommonName:(NSString *)name {
    return [NSString stringWithFormat:@"%@.png", [self correctName:name iPad:NO]];
}

- (UIImage *)loadCommonImage:(NSString *)name {
    return [UIImage imageNamed:[self imageName:name iPad:NO]];
}

- (UIImage *)middleStretchCommonImage:(NSString *)name {
    return [Utils middleStretchImagedName:[self imageName:name iPad:NO]];
}

/* xibs or story boards */
- (id)loadViewNib:(NSString *)nibName {
    NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:[self correctName:nibName iPad:YES] owner:nil options:nil];
    if([nibs count] > 0) {
        return nibs[0];
    }
    return nil;
}

- (id)loadViewControllerNib:(NSString *)nibName {
    UIViewController *viewController = [[NSClassFromString(nibName) alloc] initWithNibName:[self correctName:nibName iPad:YES] bundle:nil];
    return viewController;
}

- (id)loadStoryBoard:(NSString *)storyName {
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:[self correctName:storyName iPad:YES] bundle:nil];
    return storyBoard;
}

- (id)loadViewController:(NSString *)name inStory:(NSString *)story {
    UIStoryboard *storyBoard = [self loadStoryBoard:story];
    return [storyBoard instantiateViewControllerWithIdentifier:name];
}

@end
